package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.User_kService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;

@RestController
@RequestMapping("/user_k")
public class User_kController {

    @Reference
    private User_kService userService;

    //获取当前登录（认证）用户的用户名
    @RequestMapping("/getLoginUsername")
    public Result getLoginUsername(){
        try{
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = user.getUsername();
            String password = user.getPassword();
            Collection<GrantedAuthority> authorities = user.getAuthorities();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
        }catch (Exception e){
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

    //新增用户
    @RequestMapping("/add")
    public Result add(@RequestBody com.itheima.pojo.User user, Integer[] roleIds) {
        try {
            userService.add(user, roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增用户失败");
        }
        return new Result(true, "新增用户成功");
    }

    //根据id查询对应的用户信息  用于编辑用户信息回显
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            com.itheima.pojo.User user = userService.findById(id);
            return new Result(true, "查询用户成功", user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询用户失败");
        }

    }

    //根据id查询对应的用户信息  用于编辑用户信息回显
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return userService.findPage(queryPageBean);
    }

    //编辑用户信息
    @RequestMapping("/edit")
    public Result edit(@RequestBody com.itheima.pojo.User user, Integer[] roleIds){
        try {
            userService.edit(user,roleIds);
            return new Result(true,"编辑用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,"编辑用户失败");
        }
    }

    @RequestMapping("/deleteById")
    public Result deleteById(Integer userId){
        try {
            userService.deleteById(userId);
            return new Result(true,"删除用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, "删除用户失败");
        }

    }
}
